﻿using System;
using System.Collections.Generic;
using System.IO;

namespace SpellCheckerV1
{
    static public class Checker
    {

        public static bool SpellChecker(string s)
        {
            var filePath = @"C:\Dokumente und Einstellungen\Administrator\Eigene Dateien\Visual Studio 2008\Projects\SpellCheckerTests\SpellCheckerV1\DictionaryFile.txt";
            
            IDictionary<string, bool> dictionary = Load(filePath);

            string stringClean = CleanText(s);

            IEnumerable<string> splitted = SplitIntoWords(stringClean);
           
            return CheckWords(splitted, dictionary);
        }


        public static IDictionary<string, bool> ExpandDictionary(IDictionary<string, bool> dictionary)
        {
            IDictionary<string, bool> dictionaryExpanded = new Dictionary<string, bool>();
            
            foreach (var word in dictionary)
            {
                string[] allEntriesInWord = word.Key.Split(',');
                string wordLexeme = allEntriesInWord[0];

                if (!dictionary.ContainsKey(allEntriesInWord[0]))
                {
                    dictionaryExpanded.Add(allEntriesInWord[0], true);

                    if (allEntriesInWord.Length > 1)
                    {
                        for (int i = 1; i < allEntriesInWord.Length; i++)
                        {
                            var wordMorpheme = allEntriesInWord[i].Substring(2);
                            string completeWord = wordLexeme + wordMorpheme;
                           
                            dictionaryExpanded.Add(completeWord, true);
                        }
                    }
                }
            }
            return dictionaryExpanded;
        }


        public static IDictionary<string, bool> Load(string filePath)
        {
            IDictionary<string, bool> dictionary = new Dictionary<string, bool>();
            using (StreamReader fileDictionary = File.OpenText(filePath))
            {
                var s = "";
                while ((s = fileDictionary.ReadLine()) != null)
                {
                    dictionary.Add(s, true);
                }
                fileDictionary.Close();
            }
            return dictionary;
        }


        public static string CleanText(string s)
        {
            return s.Trim(' ');
          
        }


        public static IEnumerable<string> SplitIntoWords(string s)
        {
            var delimiters = new char[] { '\r', '\n', '\r', ' ', '"', '\'', ',', ';', ':', '.', '!', '?' };
            IEnumerable<string> stringSplitted = s.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            return stringSplitted;
        }    


        public static bool CheckWords(IEnumerable<string> stringSplitted, IDictionary<string, bool> dictionary)
        {
            var errorFound = false;
            foreach (string s in stringSplitted)
            {
                if (!dictionary.ContainsKey(s))
                    errorFound = true;
            }
            return errorFound;
        }
    }
}
